import Vue from "vue";

import Vuex from "vuex";
import { get_userInfo } from "../api/user";
Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    token: "",
    tokenExpired: "",
    uid: "",
    username: "",
    userInfo: {},
  },
  mutations: {
    //通用的提交数据的方法
    change(state, { key, value }) {
      state[key] = value;
    },
  },
  actions: {
    //请求用户信息
    async get_userInfo({ commit }) {
      let token = uni.getStorageSync("token");
      return get_userInfo({ token }).then((res) => {
        console.log(res);
        if (res.code == 0) {
          commit("change", {
            key: "isLogin",
            value: true,
          });
          commit("change", {
            key: "userInfo",
            value: res.userInfo,
          });
          commit("change", {
            key: "uid",
            value: res.uid,
          });
        } else {
          console.log(111);
        }
      });
    },
  },
});
export default store;
